'''
Created on 2011-07-25

@author: Mikael
'''

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from View.Widgets.EntityEditor.ComponentEditor import ComponentEditor
from Model.EditorPath import EditorPath
from View.Widgets.EntityEditor.ComponentWidget import ComponentWidget


class AddEntityComponentEditor(ComponentEditor):
    
    def __init__(self, entityWidget, title="", parent=None):
        ComponentEditor.__init__(self, title, parent)
        #QWidget.__init__(self)
        
        #self.mLayout = QBoxLayout(QBoxLayout.TopToBottom)
        #self.mLayout = QHBoxLayout()
        self.mLayout = QGridLayout()
        self.setLayout(self.mLayout)
        self.mEntityWidget = entityWidget
        
        
        #Material
        self.mMaterialButton = QToolButton()
        self.mMaterialButton.setIcon(QIcon(EditorPath.mImageFolderPath + EditorPath.mMaterial32ImageFile));
        self.mMaterialButton.setIconSize(QSize(32,32));
        self.mMaterialButton.setToolTip("Add material component");
        self.mMaterialButton.setCursor(Qt.PointingHandCursor)
        self.mLayout.addWidget(self.mMaterialButton, 0, 0)
        self.mLayout.addWidget(QLabel("(+)Material component"), 0, 1)
        
        
        #Transformation
        self.mTransformation = QToolButton()
        self.mTransformation.setIcon(QIcon(EditorPath.mImageFolderPath + EditorPath.mTransformation32ImageFile));
        self.mTransformation.setIconSize(QSize(32,32));
        self.mTransformation.setToolTip("Add transformation component");
        self.mTransformation.setCursor(Qt.PointingHandCursor)
        self.mLayout.addWidget(self.mTransformation, 1, 0)
        self.TransformationLabel = QLabel("(+)Transformation component")
        self.mLayout.addWidget(self.TransformationLabel, 1, 1)
        
        
        #Mesh
        self.mMesh = QToolButton()
        self.mMesh.setIcon(QIcon(EditorPath.mImageFolderPath + EditorPath.mMesh32ImageFile));
        self.mMesh.setIconSize(QSize(32,32));
        self.mMesh.setToolTip("Add mesh component");
        self.mMesh.setCursor(Qt.PointingHandCursor)
        self.mMesh.setText("Mesh")
        self.mLayout.addWidget(self.mMesh, 2, 0)
        self.mLayout.addWidget(QLabel("(+)Mesh component"), 2, 1)
        
        self.mTransformation.clicked.connect(self.transformClicked)
        
        total = 3 * 40
        
        
        self.setMinimumHeight(total + 10)
        self.setMaximumHeight(total + 10)
        
        
    def transformClicked(self):
        self.mEntityWidget.addTransformation()
        self.mTransformation.setEnabled(0)
        self.mLayout.removeWidget(self.mTransformation)
        self.mLayout.removeWidget(self.TransformationLabel)
        self.mTransformation.hide()
        self.TransformationLabel.hide()


class AddEntity(ComponentWidget):
    '''
    classdocs
    '''


    def __init__(self, entityWidget, title="", parent=None):
        '''
        Constructor
        '''
        ComponentWidget.__init__(self, title, parent)
        self.mImage         = QImage()
        self.mImage.load(EditorPath.mImageFolderPath + EditorPath.mAdd32ImageFile)
        self.mImageHover    = QImage()
        self.mImageHover.load(EditorPath.mImageFolderPath + EditorPath.mAdd32HoverImageFile)
        self.mImagePressed  = QImage()
        self.mImagePressed.load(EditorPath.mImageFolderPath + EditorPath.mAdd32PressedImageFile)
        
        self.mComponentEditor = AddEntityComponentEditor(entityWidget, "Editor", parent)
        
        
        
        
    def paintEvent(self, e):

        qp = QPainter()
        qp.begin(self)
        
        if self.mIsOpen:
            self.drawOpen(qp)
            self.drawImageOpen(qp)
        elif self.mIsHover:
            self.drawHover(qp)
            self.drawImageHover(qp)
        else:
            self.drawIdle(qp)
            self.drawImage(qp)
            
        qp.end()
        
    def drawOpen(self, QPainter):
        ###########
        # GRADIENT
        ###########
        gradientRadial = QLinearGradient(0,0, 0, self.height());
        gradientRadial.setColorAt(0.0,QColor(60,60,60));
        # Paint
        QPainter.setPen(Qt.NoPen)
        oldBrush = QPainter.brush()
        QPainter.setBrush(gradientRadial);
        QPainter.drawRect(self.rect())
        QPainter.setBrush(oldBrush);
        
    
    def drawIdle(self, QPainter):
        ###########
        # GRADIENT
        ###########
        gradientRadial = QLinearGradient(0,0, 0, self.height());
        gradientRadial.setColorAt(0.0,QColor(60,60,60));

        # Paint
        QPainter.setPen(Qt.NoPen)
        oldBrush = QPainter.brush()
        QPainter.setBrush(gradientRadial);
        QPainter.drawRect(self.rect())
        QPainter.setBrush(oldBrush);
    
    def drawHover(self, QPainter):
        ###########
        # GRADIENT
        ###########
        gradientRadial = QLinearGradient(0,0, 0, self.height());
        gradientRadial.setColorAt(0.0,QColor(60,60,60));
        # Paint
        QPainter.setPen(Qt.NoPen)
        oldBrush = QPainter.brush()
        QPainter.setBrush(gradientRadial);
        QPainter.drawRect(self.rect())
        QPainter.setBrush(oldBrush);
        